package logic

import (
	"context"
	"fmt"
	"models"
	"pkg"

	"shop_srv/internal/svc"
	"shop_srv/shop_srv"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *shop_srv.LoginRequest) (*shop_srv.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var user models.User
	if err := models.DB.Where("user_phone = ?", in.Phone).Find(&user).Limit(1).Error; err != nil {
		return nil, err
	}
	if user.ID == 0 {
		return nil, fmt.Errorf("账号不可用")
	}
	if pkg.MD5(in.Password) != user.UserPassword {
		return nil, fmt.Errorf("账号或密码错误")
	}
	return &shop_srv.LoginResponse{
		UserId: int64(user.ID),
	}, nil
}
